package service

import (
    "gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/repository"
	"gitlab.yoyiit.com/youyi/go-core/store"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"golang.org/x/net/context"
	"encoding/json"
)

type ProductCustomValueService interface {
	ListProductCustomValue(ctx context.Context, req *api.ListProductCustomValueRequest) (*api.ListProductCustomValueResponse, error)
	GetProductCustomValue(ctx context.Context, req *api.ProductCustomValueData) (*api.ProductCustomValueData, error)
	AddProductCustomValue(ctx context.Context, req *api.ProductCustomValueData) error
	EditProductCustomValue(ctx context.Context, req *api.ProductCustomValueData) error
	DeleteProductCustomValue(ctx context.Context, id int64) (err error)
	BatchDeleteProductCustomValue(ctx context.Context, req *api.BatchDeleteProductCustomValueRequest) (err error)
}

type somsService struct {
	productCustomValueRepo repo.ProductCustomValueRepo
}

func (s *somsService) ListProductCustomValue(ctx context.Context, req *api.ListProductCustomValueRequest) (*api.ListProductCustomValueResponse, error) {
	dbDatas, count, err := s.productCustomValueRepo.List(ctx, "", req.PageNum, req.PageSize, &repo.ProductCustomValueDBDataParam{
	    ProductCustomValueDBData: repo.ProductCustomValueDBData {
            SomsProductId: req.Data.SomsProductId,
			FieldId: req.Data.FieldId,
			FieldName: req.Data.FieldName,
			FieldType: req.Data.FieldType,
			PrintFlag: req.Data.PrintFlag,
			Sort: req.Data.Sort,
			Value: req.Data.Value,
			
	    },
	})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	var result []*api.ProductCustomValueData
	if dbDatas != nil {
		result = make([]*api.ProductCustomValueData, len(*dbDatas))
		for i, data := range *dbDatas {
			result[i] = &api.ProductCustomValueData{
				Id:                  data.Id,
                OrganizationId:      data.OrganizationId,
                CreatedAt:           util.FormatDateTime(data.CreatedAt),
                UpdatedAt:           util.FormatDateTime(data.UpdatedAt),
                CreatedUserId:       data.CreatedUserId,
                UpdatedUserId:       data.UpdatedUserId,
                UpdatedUserNickname: data.UpdatedUserNickname,
                CreatedUserNickname: data.CreatedUserNickname,
				SomsProductId: data.SomsProductId,
	FieldId: data.FieldId,
	FieldName: data.FieldName,
	FieldType: data.FieldType,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	Value: data.Value,
	
			}
		}
	}
	return &api.ListProductCustomValueResponse{
		Data:  result,
		Count: count,
	}, nil
}

func (s *somsService) GetProductCustomValue(ctx context.Context, req *api.ProductCustomValueData) (*api.ProductCustomValueData, error) {
	data, err := s.productCustomValueRepo.Get(ctx, &repo.ProductCustomValueDBData{
		BaseDBData: repository.BaseDBData{
        			BaseCommonDBData: repository.BaseCommonDBData{
        				Id: req.Id,
        			},
        		},
		})
	if err != nil {
		return nil, handler.HandleError(err)
	}
	resp := &api.ProductCustomValueData{
		Id:                  data.Id,
        OrganizationId:      data.OrganizationId,
        CreatedAt:           util.FormatDateTime(data.CreatedAt),
        UpdatedAt:           util.FormatDateTime(data.UpdatedAt),
        CreatedUserId:       data.CreatedUserId,
        UpdatedUserId:       data.UpdatedUserId,
        UpdatedUserNickname: data.UpdatedUserNickname,
        CreatedUserNickname: data.CreatedUserNickname,
		SomsProductId: data.SomsProductId,
	FieldId: data.FieldId,
	FieldName: data.FieldName,
	FieldType: data.FieldType,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	Value: data.Value,
	
	}
	return resp, nil
}

func (s *somsService) AddProductCustomValue(ctx context.Context, data *api.ProductCustomValueData) error {
	repoData := &repo.ProductCustomValueDBData{
		SomsProductId: data.SomsProductId,
	FieldId: data.FieldId,
	FieldName: data.FieldName,
	FieldType: data.FieldType,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	Value: data.Value,
	
	}
	_, err := s.productCustomValueRepo.Add(ctx, repoData)
	if err != nil {
		return handler.HandleError(err)
	}
	return nil
}

func (s *somsService) EditProductCustomValue(ctx context.Context, data *api.ProductCustomValueData) error {
	repoData := &repo.ProductCustomValueDBData{
		BaseDBData: repository.BaseDBData{
        			BaseCommonDBData: repository.BaseCommonDBData{
        				Id: data.Id,
        			},
        		},
		SomsProductId: data.SomsProductId,
	FieldId: data.FieldId,
	FieldName: data.FieldName,
	FieldType: data.FieldType,
	PrintFlag: data.PrintFlag,
	Sort: data.Sort,
	Value: data.Value,
	
	}
	return handler.HandleError(s.productCustomValueRepo.UpdateById(ctx, data.Id, repoData))
}

func (s *somsService) DeleteProductCustomValue(ctx context.Context, id int64) (err error) {
	return handler.HandleError(s.productCustomValueRepo.DeleteById(ctx, id))
}

func (s *somsService) BatchDeleteProductCustomValue(ctx context.Context, req *api.BatchDeleteProductCustomValueRequest) (err error) {
	if req.Ids != nil && len(req.Ids) > 0 {
		// 自行补充逻辑
	}
	return nil
}
